package com.bp.hearthealth.pulse.lnapps.bp_utils

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil

object GooglePlayUtils {

    fun openGooglePlayForRating(context: Context) {
        val packageName = context.packageName
        val uri = Uri.parse("market://details?id=$packageName")

        val intent = Intent(Intent.ACTION_VIEW, uri)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

        try {
            context.startActivity(intent)
        } catch (e: ActivityNotFoundException) {
            // 如果用户没有安装 Google Play 商店，也可以在浏览器中打开评论页
            val playStoreUrl = "https://play.google.com/store/apps/details?id=$packageName"
            LogUtil.i(" Google Play 商店 $playStoreUrl")
            openUrlInBrowser(context, playStoreUrl)
        }
    }

    private fun openUrlInBrowser(context: Context, url: String) {
        val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

        try {
            context.startActivity(browserIntent)
        } catch (e: ActivityNotFoundException) {
            // 如果没有可用的浏览器应用，可以提醒用户
            // 或者在生产环境中可能不需要处理此情况
        }
    }
}
